<template>
 <div>
<el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>学科管理</el-breadcrumb-item>
  <el-breadcrumb-item>学科列表</el-breadcrumb-item>
  
</el-breadcrumb>
<!--添加卡片-->
<el-card >
<!--搜索与添加-->
<div style="margin-top: 15px;">

  

  <el-row :gutter="30">
  <el-col :span="8">
 <el-input placeholder="请输入想要搜索的学科(支持模糊搜索)" v-model="input3" class="input-with-select">
    
    <el-button slot="append" icon="el-icon-search" @click="SearchSubjectList(1,4)"></el-button>
  </el-input>

  
  
  </el-col>
  <el-col :span="4">
  <el-button 
  type="primary"
  @click="OpenAddDialog"
  >添加学科</el-button>
  </el-col>
  
</el-row>
<!--表格开始-->
<el-table
        :data="SubjectList"
        border
        
        style="width: 100%"
      >
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="name" label="课程名称" width="180">
        </el-table-column>
        <el-table-column prop="createtime" label="创建时间" width="180" sortable>
        </el-table-column>
        
        </el-table-column>
        <el-table-column prop="updatetime" label="更新时间" width="180" sortable>


        </el-table-column>
        <el-table-column prop="questionnum" label="题目数量" width="180">
        
        </el-table-column>
         </el-table-column>
        <el-table-column prop="img_url" label="封面图片" width="180">
        
        <template slot-scope="scope">
             <el-image
      style="width: 100px; height: 100px"
      :src="scope.img_url"
      ></el-image>
        </template>
        </el-table-column>

        <el-table-column label="操作" width="190">
          <template slot-scope="scope">
            <!--修改按钮-->
            <el-tooltip
              class="item"
              effect="dark"
              content="编辑"
              placement="top"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="OpenEditDialog(scope.row.id)"
                
              ></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              content="删除"
              placement="top"
              
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                 @click="DeleteSubjectListById(scope.row.id)"
              ></el-button>
            </el-tooltip>
            
          </template>
        </el-table-column>
      </el-table>
      <!--分页功能-->
      <el-pagination

  @current-change="handleCurrentChange"
  :page-size="4"
  :pager-count="11"
  layout="prev, pager, next"
  :total="total"
  >
</el-pagination>
</div>
    
</el-card>
<!--添加学科会话框-->
<el-dialog
      title="学科信息添加"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
    <el-form
        :model="addForm"
        :rules="rules"
        ref="addFormRef"
        label-width="70px"
    >
    <!--题目-->
    <el-form-item label="学科名">
          <el-input v-model="addForm.name"></el-input>
    </el-form-item>

   

    </el-form>
    <!--底部区域-->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click = "InsertSubject('addFormRef')">确 定</el-button>
      </span>

    </el-dialog>
    <!-- 修改学科对话框-->
    <el-dialog title="修改学科"
    :visible.sync="editDialogVisible"
    width="50%"
    >
    <!-- 声明表单进行接收-->
    <el-form :model="editForm"
        :rules="rules"
        ref="editFormRef"
        label-width="70px">
    <el-form-item label="学科名">
          <el-input v-model="editForm.name"></el-input>
    </el-form-item>
    <el-form-item label="封面">
           <el-image
      style="width: 100px; height: 100px"
      :src="editForm.img_url"
      ></el-image>
      <el-upload
  class="upload-demo"
  action="http://127.0.0.1/UploadApi/UploadSubjectImgById"
  :on-preview="handlePreview"
  :on-remove="handleRemove"
  :headers="{'id':editForm.id}"
  :before-remove="beforeRemove"
  multiple
  :limit="1"
  :on-exceed="handleExceed"
  :on-success="handleSuccess"
  :file-list="fileList">
  <el-button size="small" type="primary">点击上传</el-button>
  <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
</el-upload>
    </el-form-item>

    
    </el-form>
    <!--底部功能区-->
 <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogClosed">取 消</el-button>
        <el-button type="primary" @click="EditSubjectById">确 定</el-button>
      </span>

    
    </el-dialog>
    
 </div>
 
</template>

<script>
//对外暴露接口
 export default {
name:'SubjectList', 
   data () {
     return {
       //分页一共多少数据
       total : 1,
       //分页用参数接收
       SubjectList:[],
       //添加窗口的对话框显示
       addDialogVisible:false,
       //添加窗口用表单
       addForm:{},
       rules:{
            title: [
            { required: true, message: '请填写此字段', trigger: 'blur' },
          ],

         },
         //搜索框
        input3:"",
        //搜索之后 点击分页应该
        IsSearch:false,
        //编辑用对话框显示
        editDialogVisible:false,
        //编辑用表单
        editForm:{},
        //文件上传表单
        fileList: []


     }
   },
   components: {

   },
   created(){

   },
   mounted() {
     this.GetSubjectList(1,4);

       
   },
   methods: {
       
       //初始化表格数据
       GetSubjectList(page,count){

         let that = this;
         let url = "api/GetSubjectByPage"
         let data = {
           pagenum:page,
           count:count

         }
         this.$axios.post(url, this.$qs.stringify(data)).then(function(Response) {
        console.log(Response.data);
        let code = Response.data.code
        if ((code = 200)) {
          that.SubjectList = Response.data.data.data
          that.total = Response.data.data.count
    
        } else {
          that.$message.error(message);
        }
      });

       },
       //点击添加学科用触发函数
      OpenAddDialog(){
        this.addDialogVisible = true;
      },
      //点击编辑按钮弹框
      OpenEditDialog(id){
         
         //向后台请求最新数据
         let that = this;
         let url = "/api/GetSubjectById";
         let data = {
           id:id
         }
        this.$axios.post(url, this.$qs.stringify(data)).then(function(Response) {
        console.log(Response.data);
        let message = Response.data.message;
        let code = Response.data.code;
        let data = Response.data.data;
        if ((code = 200)) {
          //同步到editForm
          that.editDialogVisible = true;
          that.editForm = Response.data.data
          
        } else {
          that.$message.error(message);
        }
      });


      },
      //添加界面关闭的触发函数
      addDialogClosed(){
        this.$refs.addFormRef.resetFields();
        this.addDialogVisible = false
      },
      //修改界面的关闭函数
      editDialogClosed(){
        this.$refs.editFormRef.resetFields();
        this.editDialogVisible = false
      },
      //添加学科信息到数据库
      InsertSubject(addForm){
       
        
          
            //验证通过提交给后台
            let that = this;
            let url = "/api/AddSubject";
            let data = {
              name:this.addForm.name
            }
            this.$axios.post(url,
                      this.$qs.stringify(data), 
                      ).then(function(Response) {
        console.log(Response.data);
        let code = Response.data.code
        if ((code == 200)) {
          that.addDialogClosed();
          
         that.GetSubjectList(1,4);
          
        } else {
          that.$message.error( Response.data.message);
        }
      });


         
      

      },
      //分页按钮点击函数
    handleCurrentChange(val) {
        console.log("当前"+val)
        if(!this.IsSearch){
          this.GetSubjectList(val,4);

        }
        else{
          this.SearchSubjectListBySubjectNameByPage(val,4);
        }
       

      },
      //搜索函数
      SearchSubjectList(pagenum,count){
        console.log(this.input3)
        let name = this.input3
        let that = this;
        let url = "api/SearchSubjectListBySubjectNameByPage"
        let data ={
          pagenum:pagenum,
          count:count,
          name:name,


        }
         this.$axios.post(url, this.$qs.stringify(data)).then(function(Response) {
        console.log(Response.data);
        let code = Response.data.code
        if ((code = 200)) {
          that.SubjectList = Response.data.data.data
          that.total = Response.data.data.count
          that.IsSearch = true
    
        } else {
          that.$message.error(message);
        }
      });
      


        

      },
      //删除函数
      DeleteSubjectListById(id){
        console.log(id)
        //想后端发送数据
        let that = this;
        let url = "api/DeleteSubjectListById";
        let data = {
          id:id
        }
         this.$axios.post(url, this.$qs.stringify(data)).then(function(Response) {
        console.log(Response.data);
        let message = Response.data.message;
        let code = Response.data.code;
        let data = Response.data.data;
        if ((code = 200)) {
         this.handleCurrentChange(1);
         that.$message.success(message)
        } else {
          that.$message.error(message);
        }
      });


      },
      //上传文件成功的触发函数
      handleSuccess(res){
        console.log(res)
        if(res.code == 200){
             
             this.editForm.img_url = res.data

        }
     

      },
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${ file.name }？`);
      },
      //修改学科列表触发函数
      EditSubjectById(){
         let that = this;
         let url = "api/EditSubjectById"
         let data = {
           id:this.editForm.id,
           url:this.editForm.img_url,
           name:this.editForm.name,

         }
         this.$axios.post(url, this.$qs.stringify(data)).then(function(Response) {
        console.log(Response.data);
        let code = Response.data.code
        if ((code = 200)) {

          that.editDialogClosed()
          that.handleCurrentChange(1)
    
        } else {
          that.$message.error(message);
        }
      });


      }



   },
   
 }
</script>

<style scoped>

 
</style>
